﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.TraHang
{
    public partial class frmTraHang : Form
    {
        private int _phien_ThietBiBHId;
        Phien_ThietBiBH _thisPhien_ThietBiBH;
        private string _maThietBiBH;
        private double _chiPhi = 0;
        private frmDsTraThietBiBHSC _callerForm;


        public frmTraHang(int inPhien_ThietBiBHId, string inMaThietBiBH, frmDsTraThietBiBHSC inCallerForm)
        {
            InitializeComponent();
            _phien_ThietBiBHId = inPhien_ThietBiBHId;
            _maThietBiBH = inMaThietBiBH;
            groupBox1.Text = "Thiết bị: " + _maThietBiBH;
            this.Text = "Thiết bị: " + _maThietBiBH;
            _callerForm = inCallerForm;
            _thisPhien_ThietBiBH = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById(_phien_ThietBiBHId);
            if (_thisPhien_ThietBiBH.ChiPhi != null)
            {
                _chiPhi = (double)_thisPhien_ThietBiBH.ChiPhi;
            }
            else
            {
                _chiPhi = 0;
            }
        }

        private void frmTraHang_Load(object sender, EventArgs e)
        {
            
            txtNhanVienTra.Text = frmMain.CurrentNhanVien.Id.ToString();
            txtNgayTra.Text = DateTime.Now.ToString("dd/MM/yyyy - HH:mm");

            txtChiPhi.Text = _chiPhi.ToString();
            txtPhaiTraLai.Text = (0 - _chiPhi).ToString();
            
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            //Kiem tra da thu tien chua
            double PhaiTraLai = Convert.ToDouble(txtPhaiTraLai.Text);
            if(PhaiTraLai < 0)
            {
                MessageBox.Show("Phải thu đủ chi phí từ khách hàng!", "Trả hàng", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //Chuyen trang thai Phien_ThietBiBH thanh da tra
            Phien_ThietBiBH objPhien_TBHH = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById(_phien_ThietBiBHId);
            objPhien_TBHH.TrangThaiThietBiBHID = (int)MyCommon.TrangThaiThietBiBH.DaTra;
            objPhien_TBHH.ThoiDiemTra = DateTime.Now;
            objPhien_TBHH.NhanVienTraId = frmMain.CurrentNhanVien.Id;
            Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(objPhien_TBHH);
            PhienBO obj = new PhienBO();
            //Neu tat ca hang da tra thi chuyen Phien thanh hoan thanh
            if (obj.IsPhienComplete(objPhien_TBHH.PhienId))
            {
                Phien objPhien = obj.GetPhienById(objPhien_TBHH.PhienId);
                objPhien.TrangThaiPhienId = (int)MyCommon.TrangThaiPhien.DoiBaoHanh_HoanThanh;
                objPhien.ThoiDiemHoanThanh = DateTime.Now;
                obj.UpdatePhien(objPhien);
            }
            //Luu lai lich su bao hanh
            LichSuBH LichSu = new LichSuBH();
            LichSu.LoaiLichSu = (int)MyCommon.LoaiLichSu.LichSuHeThong;
            LichSu.Phien_ThietBiBHId = _phien_ThietBiBHId;
            LichSu.ThoiGian = DateTime.Now;
            LichSu.NVThucHienId = frmMain.CurrentNhanVien.Id;
            LichSu.NoiDung = "Trả thiết bị cho khách hàng";
            LichSuBHBO.getInstance.InsertLichSuBH(LichSu);
            _callerForm.RefreshForm();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtKhachDua_TextChanged(object sender, EventArgs e)
        {
            double TraLaiKH = 0;
            double KhachDua = 0;
            if (txtKhachDua.Text != "")
            {
                KhachDua = Convert.ToDouble(txtKhachDua.Text);
            }
            TraLaiKH = KhachDua - _chiPhi;
            txtPhaiTraLai.Text = TraLaiKH.ToString();
        }
        
    }
}
